package com.dhcc.hikari.controller;

import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

import org.apache.commons.lang3.time.StopWatch;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.dhcc.hikari.entity.NurseRevocation;
import com.dhcc.hikari.entity.vo.Result;
import com.dhcc.hikari.entity.vo.ResultCode;
import com.dhcc.hikari.excel.listener.DemoDataListener;
import com.dhcc.hikari.service.NurseRevocationService;
import com.dhcc.hikari.util.LoadDataInFileUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Controller
@RequestMapping("/nr")
@Api(tags = " 护士注销模块")
public class NrController {

	@Autowired
	private NurseRevocationService nurseRevocationService;

	@ResponseBody
	@RequestMapping(value = "/findById/{id}", method = RequestMethod.GET)
	@ApiOperation(value = "根据id查询")
	public Result findById(@PathVariable String id) {
		return new Result(ResultCode.SUCCESS, nurseRevocationService.findById(id));
	}

	@ResponseBody
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	@ApiOperation(value = "保存")
	public Result save(@RequestBody NurseRevocation nurseRevocation) {
		nurseRevocationService.save(nurseRevocation);
		return new Result(ResultCode.SUCCESS);
	}

	@ResponseBody
	@RequestMapping(value = "/batchImp/{fileName}", method = RequestMethod.GET)
	@ApiOperation(value = "批量插入")
	public Result batchImp(@PathVariable String fileName) {
		// 获取excel数据
		// 调用easyExcel
		String filePath = "D://excel//" + fileName + ".xlsx";
		// 这里默认读取第一个sheet
		EasyExcel.read(filePath, NurseRevocation.class, new DemoDataListener()).sheet().doRead();
		return new Result(ResultCode.SUCCESS);
	}

	@ResponseBody
	@RequestMapping(value = "/batchExp/{fileName}", method = RequestMethod.GET)
	@ApiOperation(value = "批量导出")
	public Result batchExp(@PathVariable String fileName) {
		// 先查询
		List<NurseRevocation> nrList = nurseRevocationService.findList(new NurseRevocation());
		// 写法
		String filePath = "D://excel//" + fileName + ".xlsx";
		ExcelWriter excelWriter = EasyExcel.write(filePath, NurseRevocation.class).build();
		WriteSheet writeSheet = EasyExcel.writerSheet("模板").build();
		excelWriter.write(nrList, writeSheet);
		/// 千万别忘记finish 会帮忙关闭流
		excelWriter.finish();
		return new Result(ResultCode.SUCCESS, fileName);
	}

	public void batchSave(List<NurseRevocation> nrList) {
		nurseRevocationService.batchSave(nrList);
	}

	public int batchInsert(List<NurseRevocation> nrList) {
		return nurseRevocationService.batchInsert(nrList);
	}

	// 数据库名。
	public static final String DATA_BASE_NAME = "pe_data";
	// 表名。
	public static final String TABLE_NAME = "t_nurse_revocation_v1";
	// 要插入数据的列名。（必须与插入的数据一一对应）
	public static final String COLUMN_NAME[] = {"id","name", "id_card", "first_reg_date", "allow_org", "decision_date"};
	@ResponseBody
	@RequestMapping(value = "/loadInsert", method = RequestMethod.GET)
	@ApiOperation(value = "load批量")
	public Result loadInsert() {
//	 	StopWatch stopWatch = new StopWatch();
//        stopWatch.start();
        StopWatch stopWatch = StopWatch.createStarted(); //创建后立即start，常用
        LoadDataInFileUtil loadDataInFileUtil = new LoadDataInFileUtil();
        StringBuilder sb = new StringBuilder();
//        List<Test> list = new ArrayList<>();
        String id = UUID.randomUUID().toString();
        for(int i = 0; i < 1000000; i++) {
        	loadDataInFileUtil.builderAppend(sb, id+ i);
            loadDataInFileUtil.builderAppend(sb, id+"name"+ i);
            loadDataInFileUtil.builderAppend(sb, id+"id_card"+ i);
            loadDataInFileUtil.builderAppend(sb, id+"first_reg_date"+ i);
            loadDataInFileUtil.builderAppend(sb, id+"allow_org"+ i);
            loadDataInFileUtil.builderEnd(sb, id +"decision_date"+ i);
        }
        String sql = loadDataInFileUtil.assembleSql(DATA_BASE_NAME, TABLE_NAME, COLUMN_NAME);
        int insertRow = loadDataInFileUtil.fastInsertData(sql, sb);
        System.out.println("insert应收报表数量insertRow："+insertRow);
        stopWatch.split();
        stopWatch.stop();
        System.out.println("花费时间" +  stopWatch.getTime(TimeUnit.SECONDS)+ "s");
        System.out.println("---------方法执行结束--------------");
        return new Result(ResultCode.SUCCESS);
	}

}
